package com.hunan.api.catv.service.impl.tencent_cloud.im;

import com.google.gson.JsonObject;
import com.hunan.api.catv.utils.GsonUtils;
import com.hunan.api.catv.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
public class ImService {

    @Autowired
    RestTemplate restTemplate;

    public JsonObject importAccount(String domain, String appId, String identifier, String sign, String account, String nickName, String faceUrl) {
        StringBuffer url = new StringBuffer();
        url.append(domain);
        url.append("/v4/im_open_login_svc/account_import?sdkappid={appId}&identifier={identifier}&usersig={sign}&random={random}&contenttype=json");
        Map<String, String> uriVariables = new HashMap<String, String>();
        uriVariables.put("appId", appId);
        uriVariables.put("identifier", identifier);
        uriVariables.put("sign", sign);
        uriVariables.put("random", RandomUtils.getRandomNum(32));
        JsonObject body = new JsonObject();
        body.addProperty("Identifier", account);
        body.addProperty("Nick", nickName);
        body.addProperty("FaceUrl", faceUrl);
        HttpEntity<String> requestEntity = new HttpEntity<String>(GsonUtils.toJson(body));
        ResponseEntity<String> result = restTemplate.exchange(url.toString(), HttpMethod.POST, requestEntity, String.class, uriVariables);
        if (result.getStatusCodeValue() == HttpStatus.OK.value()) {
            return GsonUtils.fromJson(result.getBody(), JsonObject.class);
        }
        return null;
    }

    public JsonObject createGroup(String domain, String appId, String identifier, String sign, String type, String name) {
        StringBuffer url = new StringBuffer();
        url.append(domain);
        url.append("/v4/group_open_http_svc/create_group?sdkappid={appId}&identifier={identifier}&usersig={sign}&random={random}&contenttype=json");
        Map<String, String> uriVariables = new HashMap<String, String>();
        uriVariables.put("appId", appId);
        uriVariables.put("identifier", identifier);
        uriVariables.put("sign", sign);
        uriVariables.put("random", RandomUtils.getRandomNum(32));
        JsonObject body = new JsonObject();
        body.addProperty("Type", type);
        body.addProperty("Name", name);
        HttpEntity<String> requestEntity = new HttpEntity<String>(GsonUtils.toJson(body));
        ResponseEntity<String> result = restTemplate.exchange(url.toString(), HttpMethod.POST, requestEntity, String.class, uriVariables);
        if (result.getStatusCodeValue() == HttpStatus.OK.value()) {
            return GsonUtils.fromJson(result.getBody(), JsonObject.class);
        }
        return null;
    }

    public JsonObject destoryGroup(String domain, String appId, String identifier, String sign, String groupId) {
        StringBuffer url = new StringBuffer();
        url.append(domain);
        url.append("/v4/group_open_http_svc/destroy_group?sdkappid={appId}&identifier={identifier}&usersig={sign}&random={random}&contenttype=json");
        Map<String, String> uriVariables = new HashMap<String, String>();
        uriVariables.put("appId", appId);
        uriVariables.put("identifier", identifier);
        uriVariables.put("sign", sign);
        uriVariables.put("random", RandomUtils.getRandomNum(32));
        JsonObject body = new JsonObject();
        body.addProperty("GroupId", groupId);
        HttpEntity<String> requestEntity = new HttpEntity<String>(GsonUtils.toJson(body));
        ResponseEntity<String> result = restTemplate.exchange(url.toString(), HttpMethod.POST, requestEntity, String.class, uriVariables);
        if (result.getStatusCodeValue() == HttpStatus.OK.value()) {
            return GsonUtils.fromJson(result.getBody(), JsonObject.class);
        }
        return null;
    }
}
